package com.sd365.permission.centre.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.sd365.common.core.annotation.stuffer.IdGenerator;
import com.sd365.common.core.common.aop.MyPageInfo;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.core.context.BaseContextHolder;
import com.sd365.common.util.BeanException;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.dao.mapper.AuditMapper;
import com.sd365.permission.centre.entity.Audit;
import com.sd365.permission.centre.pojo.dto.AuditDTO;
import com.sd365.permission.centre.pojo.query.AuditQuery;
import com.sd365.permission.centre.service.AuditService;
import com.sd365.permission.centre.service.exception.UserCentreExceptionCode;
import com.sd365.permission.centre.service.util.SendMQMessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuditServiceImpl implements AuditService {
    @Autowired
    private SendMQMessageUtil sendMQMessageUtil;
    private static final String TABLE_NAME = "audit";

    @Autowired
    AuditMapper auditMapper;

    @Autowired
    private IdGenerator idGenerator;
    @Override
    public List<Audit> commonQuery(AuditQuery auditQuery) {
        try {
            List<Audit> audits = auditMapper.commonQuery(auditQuery);
            Page page = (Page) audits;
            BaseContextHolder.set("pageInfo", JSON.toJSONString(new MyPageInfo(page.getTotal(), page.getPages())));
            return audits;
        } catch (Exception e) {
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_COMPANY_COMMONQUERY_EXCEPTION_CODE, e);
        }
    }

    @Override
    public AuditDTO queryById(Long id) {
        try {
            AuditDTO auditDTO = null;
            Audit audit = auditMapper.selectById(id);
            if (audit != null) {
                auditDTO = BeanUtil.copy(audit, AuditDTO.class);
            }
            return auditDTO;
        } catch (BeanException ex) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_BEAN_COPY_EXCEPTION, ex);
        }
    }

    /**
     * 通过是否使用简单密码查询
     * @param isusp
     * @return
     */
    @Override
    public List<AuditDTO> selectByUseSimplePwd(Byte isusp) {
        try {
            List<AuditDTO> auditDTOS = null;
            List<Audit> audits = auditMapper.selectByUseSimplePwd(isusp);
            if (audits != null) {
                auditDTOS = BeanUtil.copyList(audits, AuditDTO.class);
            }
            return auditDTOS;
        } catch (BeanException ex) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_BEAN_COPY_EXCEPTION, ex);
        }
    }

    /**
     * 通过是否长期未更新密码查询
     * @param isnup
     * @return
     */
    @Override
    public List<AuditDTO> selectByNoUpdatePwd(Byte isnup) {
        try {
            List<AuditDTO> auditDTOS = null;
            List<Audit> audits = auditMapper.selectByNoUpdatePwd(isnup);
            if (audits != null) {
                auditDTOS = BeanUtil.copyList(audits, AuditDTO.class);
            }
            return auditDTOS;
        } catch (BeanException ex) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_BEAN_COPY_EXCEPTION, ex);
        }
    }

    /**
     * 通过是否长期未登录查询
     * @param islnl
     * @return
     */
    @Override
    public List<AuditDTO> selectByLongNoLogin(Byte islnl) {
        try {
            List<AuditDTO> auditDTOS = null;
            List<Audit> audits = auditMapper.selectByLongNoLogin(islnl);
            if (audits != null) {
                auditDTOS = BeanUtil.copyList(audits, AuditDTO.class);
            }
            return auditDTOS;
        } catch (BeanException ex) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_BEAN_COPY_EXCEPTION, ex);
        }
    }
}
